package com.example.leetcode;

/**
 * 递归 & 动态规划
 * T0 = 0, T1 = 1, T2 = 1, 且在 n >= 0 的条件下 Tn+3 = Tn + Tn+1 + Tn+2
 */
public class Demo1137 {
    public static void main(String[] args) {

    }

    //递归  ** 超时
    public static int tribonacci(int n) {
        if (n<=1){
            return n;
        }
        if (n==2){
            return 1;
        }

        return tribonacci(n-1)+tribonacci(n-2)+tribonacci(n-3);
    }

    //动态规划
    public static int tribonacci1(int n) {
        if (n<=1){
            return n;
        }
        if (n==2){
            return 1;
        }
        int fun[] = new int[n+1];
        fun[0]=0;
        fun[1]=1;
        fun[2]=1;

        for (int i = 3; i < n + 1; i++) {
            fun[i]=fun[i-1]+fun[i-2]+fun[i-3];
        }
        return fun[n];
    }
}
